var combinationSum3 = function(k, n) {

  const ans = [], path = [];
  backtracking(1, 0, k, ans, path, n);
  return ans;
};

function backtracking(startIndex, sum, k, ans, path, n) {
  if (sum === n && path.length === k) {
    ans.push([...path]);
    return;
  }

  for (let i = startIndex; i <= 9; i++) {
    if (sum + i > n || path.length >= k) return;
    path.push(i);
    backtracking(i + 1, sum + i, k, ans, path, n);
    path.pop();
  }
}

const k = 3, n = 7;
console.log(combinationSum3(k, n))